package com.zzx.boot.controller;

import com.zzx.boot.entity.dto.*;
import com.zzx.boot.entity.table.User;
import com.zzx.boot.entity.table.UserBind;
import com.zzx.boot.entity.table.UserInfo;
import com.zzx.boot.entity.vo.TokenVo;
import com.zzx.boot.entity.vo.UserInfoVo;
import com.zzx.boot.entity.vo.UserVo;
import com.zzx.boot.servlet.UserInfoService;
import com.zzx.boot.servlet.UserService;
import com.zzx.boot.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * @author tan
 * @version 1.0
 * @date 2023/7/7 15:01
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Api(tags = "用户模块")
public class UserController {

    private final UserService userService;
    private final UserInfoService userInfoService;

    /**
     * 获取验证码
     * @param phone
     * @return
     */
    @ApiOperation(value = "获取验证码")
    @PostMapping("/{phone}/code")
    public R getCode(@PathVariable("phone") String phone){
        return  userService.getCode(phone);
    }

    /**
     * 手机号登录
     * @param phoneLogin
     * @return
     */
    @ApiOperation(value = "通过手机号登录注册")
    @PostMapping("/phone/login")
    public R phoneLogin(@Valid @RequestBody PhoneLoginDto phoneLogin){
        return userService.phoneLogin(phoneLogin.getPhone(),phoneLogin.getCode());
    }

    @ApiOperation(value = "通过用户名,邮箱,手机号登录")
    @PostMapping("/login")
    public R login(@Valid @RequestBody LoginDto loginDto){
        return userService.securityLogin(loginDto.getUsername(),loginDto.getPassword());
    }

    @ApiOperation(value = "第三方登录")
    @PostMapping("/bind/login")
    public R bindLogin(@Valid @RequestBody BindLoginDto loginDto){
        UserBind userBind = userService.bindLogin(loginDto);
        // 获取用户
        User user = userService.getById(userBind.getUserId());
        TokenVo tokenVo = userService.createLoginUser(user);
        return R.success(tokenVo);
    }



    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public R logout(){
        return userService.logout();
    }

    @ApiOperation("搜索用户")
    @GetMapping("/search")
    public R userSearch(String keyword){
        return R.success(userService.userSearch(keyword));
    }

    @ApiOperation("绑定用户(需要权限)")
    @PostMapping("/bind/user")
    public R bindUser(@Valid @RequestBody BindLoginDto loginDto){
        return R.success(userService.bindUser(loginDto));
    }

    @ApiOperation("绑定手机号")
    @PostMapping("/bind/phone")
    public R bindPhone(@Valid @RequestBody BindPhoneDto bindPhoneDto){
        return userService.bindPhone(bindPhoneDto.getPhone(),bindPhoneDto.getCode(),bindPhoneDto.getBindId());
    }


    @ApiOperation("编辑头像")
    @PutMapping("/update/avatar")
    public R updateAvatar(MultipartFile file){
        String str = userService.updateAvatar(file);
        return R.success(str);
    }

    @ApiOperation("修改资料")
    @PutMapping("/update/info")
    public R updateInfo(@RequestBody UserInfoDto userInfoDto){
        String str = userInfoService.updateInfo(userInfoDto);
        return R.success(str);
    }

    @ApiOperation("获取资料")
    @GetMapping("/get/info")
    public R getUserInfo(){
        UserInfoVo userInfo = userInfoService.getUserInfo();
        return R.success(userInfo);
    }


    @ApiOperation("修改密码")
    @PutMapping("/update/password")
    public R updatePassword(@Valid @RequestBody PasswordDto passwordDto){
        String str = userService.updatePassword(passwordDto.getOldPassword(),passwordDto.getNewPassword(),passwordDto.getNewPassword2());
        return R.success(str);
    }




}
